package com.dyb;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理统一异常的handler
 *
 * @author Administrator
 * @ControllerAdvice拦截控制器的异常
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    //打印错误的日志
    private final Logger log = LoggerFactory.getLogger(
            GlobalExceptionHandler.class);

    //拦截校检错误异常
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Object handleMethodArgumentNotValidException(
            MethodArgumentNotValidException e) {
        log.error("MethodArgumentNotValidException", e);
        return handleBindingResult(e.getBindingResult());
    }

    private Map<String, Object> handleBindingResult(BindingResult result) {
        //把异常处理为对外暴露的提示
        List<String> list = new ArrayList<>();
        //hasErrors()返回异常是否包含错误信息
        if (result.hasErrors()) {
            //获取异常错误信息，并添加到list集合中
            List<ObjectError> allErrors = result.getAllErrors();
            for (ObjectError objectError : allErrors) {
                list.add(objectError.getDefaultMessage());
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        //返回校检错误的信息
        map.put("code", 10002);
        map.put("message", list.toString());
        return map;
    }

}